*************************************
*** Panel Analysis: PSID *************
*************************************

clear
global OUT "SET_YOUR_OUTPATH"

// this is for data which is later read into R
global OUT2 "SET_YOUR_OUTPATH_FOR_DATAFRAMES"	



*create dummy datasets for regsave
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\PSID\psid_coef_hhinc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\PSID\psid_coef_hhinc_dec_pp_sqrt.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\PSID\psid_coef_loginc.dta", replace

*bivariate
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\PSID\psid_coef_hhinc_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\PSID\psid_coef_hhinc_dec_pp_sqrt_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\PSID\psid_coef_loginc_0.dta", replace


*equivalence testing
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\PSID\psid_coef_hhinc_dec_pp_sqrt_e.dta", replace



*load data
use "SET_INPATH\PSID\Data\psid_ta_fam_long.dta", clear


xtset pid year, delta(2)

* remove outliers
replace hhinc=. if hhinc>1000000 & hhinc<9999999

*use hhinc with standardized hhinc

egen zhhinc = std(hhinc)
replace hhinc = zhhinc


// generate loginc
gen loginc = ln(hhinc_pp_sqrt)

*create additional interaction variables
gen hhincXage = hhinc * age18
gen hhinc_ppXage = hhinc_pp * age18
gen hhinc_pp_sqrtXage = hhinc_pp_sqrt * age18

*labels
lab var vote "Vote"

**************************************
***** Analysis ***********************
**************************************

estpost summarize vote ///
					hhinc hhinc_pp hhinc_pp_sqrt hhinc_dec hhinc_dec_pp hhinc_dec_pp_sqrt female age18 unemployed nlf edu_med edu_high  hhinc_decXage  hhinc_decXage1835
esttab using "$OUT\Tables\PSID\summary_stats_psid.rtf", b(2) cells("count mean(fmt(3)) sd(fmt(3))  min max") replace



* regressions with age (continuous)
foreach var of varlist vote {
	xthybrid `var' hhinc, clusterid(pid) full
	eststo m001_`var'
	regsave W__hhinc B__hhinc using "$OUT2\PSID\psid_coef_hhinc_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc female age18 unemployed nlf edu_med edu_high, clusterid(pid) full
	eststo m01_`var'
	regsave W__hhinc B__hhinc using "$OUT2\PSID\psid_coef_hhinc.dta", ci level(95) autoid append
	
	xthybrid `var' loginc, clusterid(pid) full
	eststo m002_`var'
	regsave W__loginc B__loginc using "$OUT2\PSID\psid_coef_loginc_0.dta", ci level(95) autoid append
	xthybrid `var' loginc female age18 unemployed nlf edu_med edu_high, clusterid(pid) full
	eststo m02_`var'
	regsave W__loginc B__loginc using "$OUT2\PSID\psid_coef_loginc.dta", ci level(95) autoid append
	
	
	xthybrid `var' hhinc_dec_pp_sqrt, clusterid(pid) full
	eststo m03_`var'
	regsave W__hhinc_dec_pp_sqrt B__hhinc_dec_pp_sqrt using "$OUT2\PSID\psid_coef_hhinc_dec_pp_sqrt_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc_dec_pp_sqrt female age18 unemployed nlf edu_med edu_high, clusterid(pid) full
	eststo m3_`var'
	regsave W__hhinc_dec_pp_sqrt B__hhinc_dec_pp_sqrt using "$OUT2\PSID\psid_coef_hhinc_dec_pp_sqrt.dta", ci level(95) autoid append
}

// for equivalence testing
foreach var of varlist vote  {
	xtreg `var' hhinc_dec_pp_sqrt10 age18 unemployed nlf edu_med edu_high, fe
	regsave hhinc_dec_pp_sqrt10 using "$OUT2\PSID\psid_coef_hhinc_dec_pp_sqrt_e.dta", ci level(90) autoid append
}



// interactions
foreach var of varlist vote  {
	xthybrid `var' hhinc_dec_pp_sqrt female age18 unemployed nlf edu_med edu_high hhinc_dec_pp_sqrtXage, clusterid(pid) full
	eststo m7_`var'
}

esttab m3_vote m7_vote using "$OUT\Tables\PSID\hybrid_vote_inc.rtf", b(3) se replace
esttab m3_vote m7_vote using "$OUT\Tables\PSID\hybrid_vote_inc.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__Female "Female" ///
				R__migrant "Migrant" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				W__edu_med "W: Edu: medium" ///
				W__edu_high "W: Edu: high" ///
				B__edu_med "B: Edu: medium" ///
				B__edu_high "B: Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhincXage "W: Inc. * Age" ///
				B__hhincXage "B: Inc. * Age" ///
				W__hhinc_pp_sqrt "W: Income (dec.)" ///
				B__hhinc_pp_sqrt "B: Income (dec.)" ///
				W__hhinc_pp_sqrtXage "W: Inc. (dec.) * Age" ///
				B__hhinc_pp_sqrtXage "B: Inc. (dec.) * Age" ///
				W__hhinc_sat "W: Sat. Income" ///
				B__hhinc_sat "B: Sat. Income" ///
				W__hhinc_satXage "W: Sat. Inc. * Age" ///
				B__hhinc_satXage "B: Sat. Inc. * Age" ///
				_cons "Constant")



*logistic model for vote	
xthybrid vote hhinc_dec_pp_sqrt female age18 unemployed nlf edu_med edu_high, clusterid(pid) family(binomial) link(logit) full
eststo m1_vote_logit_incdec
esttab  m1_vote_logit_incdec  using "$OUT\Tables\PSID\hybrid_vote_logit.rtf", b(3) se replace
esttab  m1_vote_logit_incdec  using "$OUT\Tables\PSID\hybrid_vote_logit.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__Female "Female" ///
				R__migrant "Migrant" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				W__edu_med "W: Edu: medium" ///
				W__edu_high "W: Edu: high" ///
				B__edu_med "B: Edu: medium" ///
				B__edu_high "B: Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhincXage "W: Inc. * Age" ///
				B__hhincXage "B: Inc. * Age" ///
				W__hhinc_pp_sqrt "W: Income (dec.)" ///
				B__hhinc_pp_sqrt "B: Income (dec.)" ///
				W__hhinc_pp_sqrtXage "W: Inc. (dec.) * Age" ///
				B__hhinc_pp_sqrtXage "B: Inc. (dec.) * Age" ///
				W__hhinc_sat "W: Sat. Income" ///
				B__hhinc_sat "B: Sat. Income" ///
				W__hhinc_satXage "W: Sat. Inc. * Age" ///
				B__hhinc_satXage "B: Sat. Inc. * Age" ///
				_cons "Constant")

*



